package com.lxb.myforum.controller

import javax.servlet.http.HttpServletRequest

import com.lxb.myforum.base.BaseController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.data.domain.Sort.Direction
import org.springframework.data.domain.Sort.Order
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

import com.lxb.myforum.pojo.Article
import com.lxb.myforum.pojo.Comment
import com.lxb.myforum.pojo.User
import com.lxb.myforum.service.ArticleService
import com.lxb.myforum.service.CommentService

@Controller
@RequestMapping("/articles")
class ArticleController @Autowired
constructor(
        private val articleService: ArticleService,
        private val commentService: CommentService
) : BaseController() {

    @GetMapping(value = ["", "/", "/index"])
    fun index(req: HttpServletRequest, article: Article,
              @RequestParam(value = "page", defaultValue = "1") page: Int,
              @RequestParam(value = "size", defaultValue = "10") size: Int): String {
        val pageable = PageRequest.of(page - 1, size)
        val articlesPage = articleService.findArticles(article, pageable)

        //		req.setAttribute("articles", articlesPage.getContent());
        //		req.setAttribute("totalCount", articlesPage.getTotalElements());
        //		req.setAttribute("totalPage", articlesPage.getTotalPages());
        //		req.setAttribute("curPage", articlesPage.getNumber() + 1);
        //		req.setAttribute("curSize", articlesPage.getSize());
        logger.debug(articlesPage)
        req.setAttribute("articlesPage", articlesPage)

        return "/articles/index"
    }

    @GetMapping(value = ["/{id}", "/{id}/detail"])
    fun detail(req: HttpServletRequest,
               @PathVariable("id") id: Long,
               @RequestParam(value = "page", defaultValue = "1") page: Int,
               @RequestParam(value = "size", defaultValue = "10") size: Int): String {
        val article = articleService.findById(id)
        req.setAttribute("article", article)

        val o = Order(Direction.DESC, "createTime")
        val pageable = PageRequest.of(page - 1, size, Sort.by(o))
        val comment = Comment()
        comment.article = article
        val commentsPage = commentService.findComments(comment, pageable)

        //		req.setAttribute("comments", commentsPage.getContent());
        //		req.setAttribute("totalCount", commentsPage.getTotalElements());
        //		req.setAttribute("totalPage", commentsPage.getTotalPages());
        //		req.setAttribute("curPage", commentsPage.getNumber() + 1);
        //		req.setAttribute("curSize", commentsPage.getSize());
        logger.debug(commentsPage)
        req.setAttribute("commentsPage", commentsPage)

        return "/articles/detail"
    }

    @PostMapping(value = ["/{articleId}/comments"])
    @ResponseBody
    fun addComment(req: HttpServletRequest, @PathVariable("articleId") articleId: Long, comment: Comment): Comment {

        val o = SecurityContextHolder.getContext().authentication.principal
        if (o is User) {
            comment.user = o
        }
        val article = Article(articleId)
        comment.article = article
        return commentService.saveComment(comment)
    }

    @GetMapping(value = ["/edit", "/write"])
    fun edit(): String {
        return "/articles/edit"
    }

    @RequestMapping(value = ["", "/"], method = [RequestMethod.POST, RequestMethod.PUT])
    fun save(article: Article): String {
        val o = SecurityContextHolder.getContext().authentication.principal
        if (o is User) {
            article.user = o
        }
        val savedArticle = articleService.saveArticle(article)
        return "redirect:/articles/" + savedArticle.id!!
    }
}
